<template>
    <!-- <div class="app-container"> -->
      <div class="main-class">
      <el-row :gutter="20">
        <el-col :span="24" :xs="24">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="服务名称" prop="category">
              <el-input
                v-model="queryParams.category"
                placeholder="请输入服务名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
      
            <el-form-item label="价格" prop="price">
              <el-input
                v-model="queryParams.price"
                placeholder="请输入价格"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <!-- <el-form-item label="联系电话" prop="contactNumber">
              <el-input
                v-model="queryParams.contactNumber"
                placeholder="请输入联系电话"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="电子邮件" prop="email">
              <el-input
                v-model="queryParams.email"
                placeholder="请输入电子邮件"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item> -->
            <el-form-item label="地址" prop="address">
              <el-input
                v-model="queryParams.address"
                placeholder="请输入地址"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
         </el-col>
   
        </el-row>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
      
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

        <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="服务ID" align="center" prop="id" /> -->
        <el-table-column label="服务名称" align="center" prop="category" />
        <el-table-column label="服务描述" align="center" prop="description" />
        <el-table-column label="价格" align="center" prop="price" />
        <!-- <el-table-column label="联系电话" align="center" prop="contactNumber" />
        <el-table-column label="电子邮件" align="center" prop="email" /> -->
        <el-table-column label="地址" align="center" prop="address" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <Pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

      </el-row>
  
   
   
  
      <!-- 添加或修改生活服务信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="服务名称" prop="category">
            <el-input v-model="form.category" placeholder="请输入服务名称" />
          </el-form-item>

          <el-form-item label="服务描述" prop="description">
            <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="form.price" placeholder="请输入价格" />
          </el-form-item>
          <!-- <el-form-item label="联系电话" prop="contactNumber">
            <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
          </el-form-item>
          <el-form-item label="电子邮件" prop="email">
            <el-input v-model="form.email" placeholder="请输入电子邮件" />
          </el-form-item> -->
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入地址" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>

  export default {
    name: "Category",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 生活服务信息表格数据
        categoryList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          category: null,
          providerName: null,
          description: null,
          price: null,
          // contactNumber: null,
          // email: null,
          address: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          category: [
            { required: true, message: "服务名称不能为空", trigger: "blur" }
          ],
      
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询生活服务信息列表 */

      getList(){
                this.loading = true
                this.$axios.get("/system/category/list", {params: {
                  category: this.queryParams.category,
                  providerName: this.queryParams.providerName,
                  description: this.queryParams.description,
                  price: this.queryParams.price,
                  address: this.queryParams.address,
                  pageNum: this.queryParams.pageNum,
                  pageSize: this.queryParams.pageSize

      
                    }}).then(res => {
                    this.categoryList = res.data.data.records
                    this.total = res.data.data.total
                    this.loading = false
                })
            },

 
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          category: null,
          providerName: null,
          description: null,
          price: null,
          address: null
        };
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {
                    pageNum: 1,
                    pageSize: 10,
                    category: null,
                    providerName: null,
                    description: null,
                    price: null,
                    // contactNumber: null,
                    // email: null,
                    address: null
                },
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd(row) {
                this.reset();
                if (row !== undefined) {
                  this.form.id = row.id;
                }
                this.open = true;
                this.title = "添加生活服务信息";
            },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        this.$axios.get("/system/category/" + id).then(response => {
          this.form = response.data.data;
          this.open = true;
          this.title = "修改生活服务信息";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id !== undefined) {
                            this.$axios.put("/system/category", this.form).then(res => {
                                if (res.data.code === 200){
                                    this.modal.notifySuccess(res.data.data)
                                }
                                this.getList()
                            })
                        } else {
                            this.$axios.post("/system/category", this.form).then(res => {
                                if (res.data.code === 200){
                                    this.modal.notifySuccess(res.data.data)
                                }
                                this.getList()
                            })
                        }
                    }
                });
                this.open = false
            },
      /** 删除按钮操作 */
      handleDelete(row){
                const ids = row.id || this.ids
                if (ids.length > 1 && ids.indexOf(1) !== -1){
                    this.modal.notifyWarning("不可以删除生活服务信息")
                }else{
                    this.modal.confirm('是否确认删除生活服务信息？').then(function () {
                    }).then(()=>{
                        this.$axios.delete("/system/category?ids=" + ids).then(res => {
                            if (res.data.code === 200){
                                this.modal.notifySuccess(res.data.data)
                                this.getList()
                            }
                        })
                    }).catch(()=>{
                        this.getList()
                    })
                }

            },
       
    
    }
  };
  </script>
  